package com.gp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gp.dao.RoleDao;
import com.gp.entity.Role;
import com.gp.entity.User;
import com.gp.service.RoleService;
import com.gp.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 来一杯凉咖
 * @create 2022/3/11 9:02
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    /**
     * 查询所有角色
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Role> getAllRoleForPage(Integer page, Integer size) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        PageHelper.startPage(page,size);
        List<Role> allRoleList = roleDao.selectList(wrapper);
        PageInfo<Role> pageInfo = new PageInfo<>(allRoleList);
        return pageInfo;
    }
    /**
     * 增加修改
     * @param role
     * @return
     */
    @Override
    public CommonResult editRole(Role role) {
        System.out.println(role);
        int i;
        if (role.getRoleId() == null) {
            i = roleDao.insert(role);
        } else {
            i = roleDao.updateById(role);
        }
        if (i > 0) {
            return new CommonResult(200, "操作成功", null);
        } else {
            return new CommonResult(500, "操作失败", null);
        }
    }
    /**
     * 删除
     * @param roleId
     * @return
     */
    @Override
    public CommonResult deleteRoleById(Integer roleId) {
        int i = roleDao.deleteById(roleId);
        if (i > 0) {
            return new CommonResult(200, "删除成功", null);
        } else {
            return new CommonResult(500, "删除失败", null);
        }
    }

    /**
     * 模糊查询
     * @param page
     * @param size
     * @param roleName
     * @param createBy
     * @return
     */
    @Override
    public PageInfo<Role> queryRole(Integer page, Integer size, String roleName, String createBy) {
        PageHelper.startPage(page,size);
        List<Role> roleList = roleDao.selectRole(roleName,createBy);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        return pageInfo;
    }

    /**
     * 权限分配
     * @param roleId
     * @param MenuId
     * @return
     */
    @Override
    public CommonResult fenMenu(Integer roleId, Integer[] MenuId) {
         //1.删除原来角色具有的权限
//        menuMapper.deleteByRid(rid);
//        //2.添加新的权限
//        for(Integer mid:mIds){
//            menuMapper.insert(rid,mid);
//        }
        return new CommonResult(200,"分配成功",null);
    }
}
